<?php
class ClasesController extends AppController {

	var $name = 'Clases';
	var $helpers = array('html', 'Flowplayer', 'Time');

	function index() {
		$this->Clase->recursive = 0;
		$this->set('clases', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid clase', true));
			$this->redirect(array('action' => 'index'));
		}
		$this -> layout = 'flowplayer';
		$this->set('clase', $this->Clase->read(null, $id));
		$this->set('profesor', $this->Clase->Curso->Profesor->find());
	}

	function add() {
		if (!empty($this->data)) {
			$this->Clase->create();
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		$cursos = $this->Clase->Curso->find('list');
		$this->set(compact('cursos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid clase', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Clase->read(null, $id);
		}
		$cursos = $this->Clase->Curso->find('list');
		$this->set(compact('cursos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for clase', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Clase->delete($id)) {
			$this->Session->setFlash(__('Clase deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Clase was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function admin_index() {
		$this->Clase->recursive = 0;
		$this->set('clases', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid clase', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('clase', $this->Clase->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Clase->create();
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		$cursos = $this->Clase->Curso->find('list');
		$this->set(compact('cursos'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid clase', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Clase->read(null, $id);
		}
		$cursos = $this->Clase->Curso->find('list');
		$this->set(compact('cursos'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for clase', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Clase->delete($id)) {
			$this->Session->setFlash(__('Clase deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Clase was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function profesor_add($curso_id = null) {
		if (!empty($this->data)) {
			$this->Clase->create();
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('controller' => 'cursos', 'action' => 'view', $this->data['Clase']['curso_id']));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		if (!$curso_id) {
			$this -> Session -> setFlash(__('Invalid id for clase', true));
			$this -> redirect(array('profesor' => false, 'controller' => 'usuarios', 'action'=>'logout'));
		}		
		$curso = $this -> Clase -> Curso -> findById($curso_id);
		$this->set('curso', $curso);
	}

	function profesor_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid clase', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Clase->save($this->data)) {
				$this->Session->setFlash(__('The clase has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The clase could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Clase->read(null, $id);
		}
	}
	
	function profesor_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for clase', true));
			$this->redirect(array('profesor' => false, 'controller' => 'profesores', 'action'=>'index'));
		}
		$clase = $this->Clase->find();
		if ($this->Clase->delete($id)) {
			$this->Session->setFlash(__('Clase deleted', true));
			$this->redirect(array('controller' => 'cursos', 'action'=>'view', $clase['Curso']['id']));
		}
		$this->Session->setFlash(__('Clase was not deleted', true));
		$this->redirect(array('controller' => 'cursos', 'action'=>'view', $clase['Curso']['id']));
	}

}
?>